﻿using IdentityModel;
using IdentityServer4.Extensions;
using IdentityServer4.Models;
using IdentityServer4.Services;
using System.Security.Claims;

namespace Api.IdentityServer4
{
    public class ProfileService : IProfileService
    {
        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var user = UserData.GetListUsers().FirstOrDefault(p => p.Id == context.Subject.GetSubjectId());

            // 存在多个权限时
            var roleArr = user.Role.Split(",");

            var claims = new List<Claim>();
            foreach (var item in roleArr)
            {
                claims.Add(new Claim(JwtClaimTypes.Role, item));
            }

            context.IssuedClaims = claims;
        }

        public async Task IsActiveAsync(IsActiveContext context)
        {
            context.IsActive = true;
        }
    }
}
